import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static List<Socket> clientSockets = new ArrayList<>();

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动，等待客户端连接...");
            while (true) {
                try {
                    Socket clientSocket = serverSocket.accept();
                    System.out.println("有新客户端连接：" + clientSocket.getInetAddress());
                    boolean isReconnect = false;
                    for (Socket existingSocket : clientSockets) {
                        if (existingSocket.getInetAddress().equals(clientSocket.getInetAddress())) {
                            isReconnect = true;
                            break;
                        }
                    }
                    new Thread(new ClientHandler(clientSocket, isReconnect)).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}